package org.shanktech.tshare.binlogsync.engine;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.shanktech.tshare.binlogsync.model.binlog.BinlogData;
import org.shanktech.tshare.binlogsync.queue.BinlogQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransferEngine {

  private static final Logger LOGGER = LoggerFactory.getLogger(TransferEngine.class);
  
  private ExecutorService singleThreadPool =
      Executors.newSingleThreadExecutor();

  private TransferThread transferThread;

  public void start() {
    this.transferThread = new TransferThread();
    this.singleThreadPool.execute(this.transferThread);
  }

  public void stop() {
    this.singleThreadPool.shutdown();
  }

  @SuppressWarnings("unused")
  private class TransferThread implements Runnable {

    private boolean isNeeRun = true;

    @Override
    public void run() {
      while (this.isNeeRun) {
        try {
          BinlogData data = BinlogQueue.take();
          data.getTransfer().transfer(data);
        } catch (Exception e) {
          LOGGER.error("");
        }

      }
    }

    public boolean isNeeRun() {
      return isNeeRun;
    }

    public void setNeeRun(boolean isNeeRun) {
      this.isNeeRun = isNeeRun;
    }

  }
}
